var CitySelect_provice = [];
/*根据id获取对象*/

function CityGetText(id){
		var value="";
		var sheng = CitySelect_provice;
		for(var i in sheng){
			var shi = sheng[i].children;
			if(sheng[i].value==id){
				value=sheng[i].text;
				break;
			}
			for(var j in shi){
				var qu = shi[j].children;
				if(shi[j].value==id){
					value=sheng[i].text+shi[j].text;
					break;
				}
				for(var k in qu){
					if(qu[k].value==id){
						value=sheng[i].text+shi[j].text+qu[k].text;
						break;
					}
				}
			}
		}
		return value;
}

//var addrShow = $('addr-show');
function CitySelect2(prov, city, country, success) {
	var Prov = null;
	Prov = document.getElementById(prov);
	var City = null;
	if (city)
		City = document.getElementById(city);
	var Country = null;
	if (country)
		Country = document.getElementById(country);
	var that = this;

	/* 自动加载省份列表 */
	this.showProv = function() {
		var len = CitySelect_provice.length;
		Prov.length = 1;
		if (City)
			City.length = 1;
		if (Country)
			Country.length = 1;
	
		for (var i = 0; i < len; i++) {
			var provOpt = document.createElement('option');
			provOpt.innerHTML = CitySelect_provice[i]['text'];
			provOpt.value = CitySelect_provice[i]['value'];
			Prov.appendChild(provOpt);
		}
	}
	/* 根据所选的省份来显示城市列表 */
	this.showCity = function() {
		
		var val = Prov.selectedIndex - 1;
		//console.log('showCity',Prov.selectedIndex ,val)
		if (Country)
			Country.length = 1;
		if (City) {
			City.length = 1;
		}
		if (val>-1) {
			var cityLen = CitySelect_provice[val]["children"].length;
	
			for (var j = 0; j < cityLen; j++) {
				var cityOpt = document.createElement('option');
				cityOpt.innerHTML = CitySelect_provice[val]["children"][j].text;
				cityOpt.value = CitySelect_provice[val]["children"][j]['value'];
				City.appendChild(cityOpt);
			}
		}
	}
	/* 根据所选的城市来显示县区列表 */
	this.showCountry = function() {
		var val1 = Prov.selectedIndex - 1;
		var val2 = City.selectedIndex - 1;
		if (Country)
			Country.length = 1; // 清空之前的内容只留第一个默认选项
		if (val2>-1) {
			var children = CitySelect_provice[val1]["children"][val2].children;
			if (!children) {
				return;
			}
			//console.log('showCountry',Country,children)
		
			for (var n = 0; n < children.length; n++) {
				var countryOpt = document.createElement('option');
				countryOpt.innerHTML = CitySelect_provice[val1]["children"][val2]["children"][n].text;
				countryOpt.value = CitySelect_provice[val1]["children"][val2]["children"][n].value;
				Country.appendChild(countryOpt);
			}
		}
	}
	/* 选择县区之后的处理函数 */
	this.selecCountry = function() {
		if (success)
			success();
	}

	/* 点击确定按钮显示用户所选的地址 */
	this.getText = function() {
		var arr = new Array();
		var val1 = Prov.selectedIndex - 1;
		var val2 = City.selectedIndex - 1;
		var val3 = Country.selectedIndex - 1;
		//console.log("getAddr", current.prov, current.city, current.country);
		if (val1 >= 0) {
			arr.push(CitySelect_provice[val1].text);
			if (val2 >= 0) {
				arr.push(CitySelect_provice[val1]["children"][val2].text);
				if (val3 >= 0) {
					arr.push(CitySelect_provice[val1]["children"][val2]["children"][val3].text);
				}
			}
		}
		return arr.join(',');
	}

	this.getValue = function(key) {
		key=key||"value"
		var val1 = Prov.selectedIndex - 1;
		var val2 = City.selectedIndex - 1;
		var val3 = Country.selectedIndex - 1;
		if (val3 >= 0) {
			return CitySelect_provice[val1]["children"][val2]["children"][val3][key];
		} else if (val2 >= 0) {
			return CitySelect_provice[val1]["children"][val2][key];
		} else if (val1 >= 0) {
			return CitySelect_provice[val1][key];
		}
		return 0;
	}

	this.loadValue = function(cityid) {
		if (!Prov) return;
		var cur = {
			va1: 0,
			va2: 0,
			va3: 0
		}
		for (var i = 0; i < CitySelect_provice.length; i++) {
			if (CitySelect_provice[i].value == cityid) {
				cur.va1 = i + 1;
				break;
			}
			if (CitySelect_provice[i]['children'])
				for (var j = 0; j < CitySelect_provice[i]['children'].length; j++) {
					if (CitySelect_provice[i]['children'][j].value == cityid) {
						cur.va1 = i + 1;
						cur.va2 = j + 1;
						break;
					}
					//	console.log("CitySelect_provice[i]['children'][j]['children']",CitySelect_provice[i]['children'][j]['children'])
					if (CitySelect_provice[i]['children'][j]['children'])
						for (var k = 0; k < CitySelect_provice[i]['children'][j]['children'].length; k++) {
							if (CitySelect_provice[i]['children'][j]['children'][k].value == cityid) {
								cur.va1 = i + 1;
								cur.va2 = j + 1;
								cur.va3 = k + 1;
								break;
							}
						}
				}
		}
		//console.log(cur,Prov.options,	City.options)
		if (cur.va1>0) {
			Prov.options[cur.va1].selected = true;
			if (cur.va2 > 0) {
				this.showCity();
				City.options[cur.va2].selected = true;
				if (cur.va3 > 0) {
					this.showCountry();
					//console.log(Country.options)
					Country.options[cur.va3].selected = true;
				}
			}
		}
	}
	
	this.load = function(addr) {
		var arr;
		if (addr && addr != '') {
			arr = addr.split(',')
		}
		if (arr && Prov && arr.length > 0) {
			for (var i = 0; i < Prov.options.length; i++) {
				if (Prov.options[i].innerHTML.trim() == arr[0].trim()) {
					Prov.options[i].selected = true;
					this.showCity();
					if (City && arr.length > 1) {
						for (var j = 0; j < City.options.length; j++) {
							if (City.options[j].innerHTML.trim() == arr[1].trim()) {
								City.options[j].selected = true;
								this.showCountry();
								if (Country && arr.length > 2) {
									for (var k = 0; k < Country.options.length; k++) {
										//	console.log(arr[2],Country.options[k].innerHTML)
										if (Country.options[k].innerHTML.trim() == arr[2].trim()) {
											Country.options[k].selected = true;
											this.selecCountry();
											break;
										}
									}
								}
								break;
							}
						}
					}
					break;
				}
			}
		}
	}
	this.showProv();
	if (Prov)
		Prov.onchange = this.showCity;
	if (City)
		City.onchange = this.showCountry;
	if (Country)
		Country.onchange = this.selecCountry;
	return this;
}

/*
<div class="col-sm-8">
<select id="province"  class="width-25">
	<option value="">=请选择省份=</option>
</select>
<!--城市选择-->
<select id="city"  class="width-25">
	<option value="">=请选择城市=</option>
</select>

<!--县区选择-->
<select id="region"	class="width-25">
	<option value="">=请选择县区=</option>
</select>
</div>
<script>
	var cs= new CitySelect('province','city','region');
	var cs1= new CitySelect('province1','city1','region1');	
function fun1(){
	console.log("组件",cs.getAddr())
}
</script>
*/
